package com.atguigu.cloud.exception;

import com.atguigu.cloud.response.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理器
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> globalExceptionHandler(Exception e) {
        log.error("全局异常信息：{}", e.getMessage(), e);
        return ResultData.fail(e.getMessage());
    }

    /**
     * 运行时异常处理器
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> runtimeExceptionHandler(RuntimeException e) {
        log.error("运行时异常信息：{}", e.getMessage(), e);
        return ResultData.fail(e.getMessage());
    }
}
